/**
 * @Author admin
 * @Date 2019/3/29 8:14
 */

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author admin
 * @Date 2019/3/27 11:31
 */
public class ArrayTest {
    public static void main(String[] args) {
        /*Arraylist,看做长度可变的数组
         * 元素可重复，有序
         * 添加内容：add()
         * 取出内容：get()*/
        ArrayList arrayList = new ArrayList();
        arrayList.add("hello ");
        arrayList.add("java ");
        arrayList.add("mysql ");


        System.out.println(arrayList.get(0));
        System.out.println(arrayList.size());


        /*用ArrayList存取整型，Studengt*/

        ArrayList num = new ArrayList();
        int a0 = 3, a1 = 2, a3 = 5, a4 = 7;
        num.add(a0);

        Student1 student0 = new Student1("张三", 19);
        Student1 student1 = new Student1("历史", 200);

        ArrayList list3 = new ArrayList();
        list3.add(student0);
        list3.add(student1);
        /*遍历一 for：每一个索引/下标*/
        for (int i = 0; i < list3.size(); i++) {
            System.out.println(list3.get(i));
        }
        /*遍历方法二：foreach 每一个元素*/
        for (Object obj : list3) {
            System.out.println(obj);
        }
        /*遍历方法三：Iterator
         * 获取迭代器对象
         * hasNext（）判断是否含有可迭代的元素
         * next() 取出元素*/
        Iterator iterator = list3.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        /*用泛型，不需要强转，集合记得放入的是什么类型*/
        ArrayList<Student1> arrayList1=new ArrayList<Student1>();
        arrayList1.add(student0);
        arrayList1.add(student1);
        for (Student1 s:arrayList1){
            System.out.println(s);
        }
    }
}
